package com.beaven.imclient.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import com.beaven.imclient.R;
import com.beaven.imclient.base.BaseActivity;
import com.beaven.imclient.push.PushIntentService;
import com.beaven.imclient.push.PushService;
import com.beaven.imclient.utils.ToastUtil;
import com.beaven.model.model.sp.Account;
import com.igexin.sdk.PushManager;
import timber.log.Timber;

public class StartActivity extends BaseActivity {

  public static final int REQUEST_PERMISSION = 0;

  public static void start(Context context) {
    Intent intent = new Intent(context, StartActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);
  }

  @Override public int getLayoutId() {
    return R.layout.activity_start;
  }

  @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
      @NonNull int[] grantResults) {
    if (requestCode == REQUEST_PERMISSION) {
      if ((grantResults.length == 2
          && grantResults[0] == PackageManager.PERMISSION_GRANTED
          && grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
        Timber.e("获取设备权限成功");
      } else {
        Timber.e("获取权限失败");
        ToastUtil.show("未授权，应用功能可能异常");
      }
    } else {
      super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    start();
  }

  @Override protected void initBefore() {
    super.initBefore();
    // 注册消息接收
    PushManager.getInstance().initialize(this, PushService.class);
    PushManager.getInstance()
        .registerPushIntentService(this.getApplicationContext(), PushIntentService.class);
  }

  @Override protected void initData() {
    super.initData();
    // sd卡权限
    boolean sdCardWritePermission =
        getPackageManager().checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
            getPackageName()) == PackageManager.PERMISSION_GRANTED;

    // 获取imei
    boolean phoneSatePermission =
        getPackageManager().checkPermission(Manifest.permission.READ_PHONE_STATE, getPackageName())
            == PackageManager.PERMISSION_GRANTED;

    if (Build.VERSION.SDK_INT >= 23 && !sdCardWritePermission || !phoneSatePermission) {
      ActivityCompat.requestPermissions(this, new String[] {
          Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE
      }, REQUEST_PERMISSION);
    } else {
      start();
    }
  }

  private void start() {
    getWindow().getDecorView().postDelayed(new Runnable() {
      @Override public void run() {
        if (!TextUtils.isEmpty(Account.getPushId())) {
          if (Account.isLogin()) {
            MainActivity.start(StartActivity.this);
          } else {
            LoginActivity.start(StartActivity.this);
          }
          StartActivity.this.finish();
        } else {
          getWindow().getDecorView().postDelayed(new Runnable() {
            @Override public void run() {
              start();
            }
          }, 1000);
        }
      }
    }, 1000);
  }
}
